package org.fast.develop.logicflow.convert;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import lombok.Data;
import org.fast.develop.logicflow.builder.AbstractLogicFlowNodeBuilder;
import org.fast.develop.logicflow.core.meta.base.Resource;
import org.fast.develop.logicflow.core.meta.exception.LogicFlowMetaException;
import org.fast.develop.logicflow.runtime.convert.builder.DesignerModelBuilder;
import org.fast.develop.logicflow.runtime.convert.designer.model.DesignerNodeModel;
import org.fast.develop.logicflow.runtime.exception.LogicFlowException;
import org.fast.develop.logicflow.task.BrmsJavaClassTask;
import org.fast.develop.logicflow.task.BrmsJavaClassTaskBuilder;
import org.fast.develop.logicflow.task.BrmsTaskListener;

import java.util.List;
import java.util.Objects;

/**
 * @author czh
 * @version V1.0
 * @data Created on 2022-12-31 16:11
 */
@Data
public class DesignerBrmsJavaClassModelBuilder implements DesignerModelBuilder<DesignerNodeModel> {

    private final String DEFAULT_NAME = "规则任务";

    private DesignerNodeModel designerModel;

    private AbstractLogicFlowNodeBuilder logicFlowBuilder;

    @Override
    public AbstractLogicFlowNodeBuilder convert() {
        if (Objects.nonNull(this.getDesignerModel()) && Objects.nonNull(this.getLogicFlowBuilder())) {
            String name = DEFAULT_NAME;
            String ruleId = null;
            if (Objects.nonNull(designerModel.getText()) && designerModel.getText().containsKey("value")) {
                name = (String) designerModel.getText().get("value");
            }
            if (Objects.nonNull(designerModel.getProperties())) {
                if (designerModel.getProperties().containsKey("ruleId")) {
                    ruleId = (String) designerModel.getProperties().get("ruleId");
                } else {
                    throw new LogicFlowException("brmsJavaClassTask未配置ruleId，解析失败");
                }
            }
            BrmsJavaClassTaskBuilder brmsJavaClassTaskBuilder = (BrmsJavaClassTaskBuilder) logicFlowBuilder.customEvent(BrmsJavaClassTaskBuilder.class, designerModel.getId());
            return brmsJavaClassTaskBuilder
                    .name(name)
                    .taskClass(BrmsTaskListener.class)
                    .ruleId(ruleId);
        } else {
            throw new LogicFlowMetaException("参数错误");
        }

    }

    @Override
    public String getType() {
        return "brmsJavaClassTask";
    }

    @Override
    public void convert(List<JSONObject> jsonObjects, Resource resource) {
        BrmsJavaClassTask brmsJavaClassTask  = (BrmsJavaClassTask) resource;
        JSONObject startObj = new JSONObject();
        startObj.set("type", getType());
        startObj.set("resourceId", brmsJavaClassTask.getResourceId());
        JSONArray incoming = new JSONArray();
        JSONArray outgoing = new JSONArray();
        brmsJavaClassTask.getIncoming().forEach(item -> {
            incoming.add(new JSONObject().set("resourceId", item.getResourceId()));
        });
        brmsJavaClassTask.getOutgoing().forEach(item -> {
            outgoing.add(new JSONObject().set("resourceId", item.getResourceId()));
        });
        startObj.set("incoming", incoming);
        startObj.set("outgoing", outgoing);
        JSONObject properties = new JSONObject();
        properties.set("ruleId", brmsJavaClassTask.getProperties().getRuleId());
        properties.set("className", brmsJavaClassTask.getProperties().getClassName());
        startObj.set("properties", properties);
        jsonObjects.add(startObj);
    }

}
